﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using GomMinhLongBL;
using GomMinhLongDO;

namespace GomMinhLong
{
    public partial class Product : System.Web.UI.Page
    {
        GomMinhLong.ShoppingCart Cart = new GomMinhLong.ShoppingCart();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string CurrentUrl = "http://" + Request.Url.Host + Request.RawUrl;
                hplFacebook.NavigateUrl = hplFacebook.NavigateUrl + CurrentUrl;
                hplGoogle.NavigateUrl = hplGoogle.NavigateUrl + CurrentUrl;
                hplTwitter.NavigateUrl = hplTwitter.NavigateUrl + CurrentUrl;

                abc.Attributes.Add("src", "//www.facebook.com/plugins/like.php?href=" + CurrentUrl + "&send=false&layout=button_count&width=450&show_faces=false&action=like&colorscheme=light&font&height=21&appId=379138395463852");
                //abc.Attributes.Add("src", "//www.facebook.com/plugins/like.php?href=" + "http://www.facebook.com/kma.vn" + "&send=false&layout=button_count&width=450&show_faces=false&action=like&colorscheme=light&font&height=21&appId=379138395463852");

                if (Request.QueryString["ID"] != null)
                {

                    cmsSanPhamDO obj = new cmsSanPhamDO();
                    obj.SanPhamID = int.Parse(Request.QueryString["ID"].ToString());
                    obj = new cmsSanPhamBL().Select(obj);
                    imgContent.ImageUrl = "/Media/" + obj.HinhAnh;
                    imgContent.AlternateText = new cmsLoaiSanPhamBL().Select(new cmsLoaiSanPhamDO { LoaiSanPhamID = obj.LoaiSanPhamID }).TenLoaiSanPham + " - " + obj.TenSanPham;

                    hplImg.NavigateUrl = "/Media/" + obj.HinhAnh;
                    lblMaSP.Text = obj.MaSanPham;

                    if (obj.GiamGia > 0)
                        lblGiamGia.Text = string.Format("{0:#,###}", obj.GiamGia) + "%";
                    else
                        lblGiamGia.Text = "0 %";

                    if (obj.DonGia <= 0)
                        lblDonGia.Text = "Liên hệ";
                    else
                    {
                        lblDonGia.Text = string.Format("{0:#,###}", obj.DonGia) + " VNÐ";
                    }
                    lbtnAddToCart.OnClientClick = "apprise('Đặt hàng Sản phẩm thành công!', {'animate':true});";
                    //ln.Text = "<a id='showanh' Title='" + new cmsLoaiSanPhamBL().Select(new cmsLoaiSanPhamDO { LoaiSanPhamID = obj.LoaiSanPhamID }).TenLoaiSanPham + "-" + obj.MaSanPham + "-" + obj.TenSanPham + "' href='/images/" + obj.HinhAnh + "'><img alt='" + obj.TenSanPham + "'  src='/images/" + obj.HinhAnh + "' /></a>";
                    hplImg.Attributes.Add("Title", obj.TenSanPham);
                    Page.Title = obj.TenSanPham + " - " + obj.eTenSanPham + " - " + new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue;
                    lblTenSP.Text = obj.TenSanPham;
                    lblThongTinChiTiet.Text = obj.MoTaChiTiet;
                    //lblMoTa.Text = WordCutMoTa(obj.Mota);
                    lblBaoHanh.Text = obj.BaoHanh;
                    lbDonVi.Text = new cmsDonViBL().Select(new cmsDonViDO { DonViID = obj.DonViID }).DonViTinh;
                    hplLoaiSP.NavigateUrl = "/danh-muc-xe/" + obj.LoaiSanPhamID + "/" + FriendlyUrl(new cmsLoaiSanPhamBL().Select(new cmsLoaiSanPhamDO { LoaiSanPhamID = obj.LoaiSanPhamID }).TenLoaiSanPham);
                    hplLoaiSP.ToolTip = new cmsLoaiSanPhamBL().Select(new cmsLoaiSanPhamDO { LoaiSanPhamID = obj.LoaiSanPhamID }).TenLoaiSanPham;
                    hplLoaiSP.Text = new cmsLoaiSanPhamBL().Select(new cmsLoaiSanPhamDO { LoaiSanPhamID = obj.LoaiSanPhamID }).TenLoaiSanPham;
                    hplNhanHieu.Text = new cmsNhanHieuBL().Select(new cmsNhanHieuDO { NhanHieuID = obj.NhanHieuID }).TenNhanHieu;
                    Page.Title = obj.TenSanPham + "-" + new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue;

                    HtmlMeta metaDesc = new HtmlMeta();
                    metaDesc.Name = "description";
                    metaDesc.Content = obj.Mota;
                    Page.Header.Controls.Add(metaDesc);



                    hplImg.Attributes.Add("rel", "groupsp");
                    string cururl = "http://" + Request.Url.Host + Request.Url.AbsolutePath;
                    fbcm.Attributes.Add("data-href", cururl);
                    load();
                }
            }
        }
        public void load()
        {
            bindatalist(new cmsSanPhamBL().SelectCungLoai(int.Parse(Request.QueryString["ID"].ToString())));
            rpt2.DataSource = new cmsImagesBL().SelectImageBySanPhamID(int.Parse(Request.QueryString["ID"].ToString()));
            rpt2.DataBind();
        }
        protected void lbtnAddToCart_Click(object sender, ImageClickEventArgs e)
        {
            string url = Request.Url.AbsolutePath;
            try
            {
                Int64 id = Int64.Parse(Request.QueryString["ID"].ToString());
                cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = id });
                float gia = (float)obj1.DonGia;
                long ClientLoginID = 0;
                if (Session["IDDN"] != null)
                    ClientLoginID = long.Parse(Session["IDDN"].ToString());
                DataTable tb = new DataTable();
                if (Session["GioHang"] != null)
                {
                    Cart.setTable((DataTable)Session["GioHang"]);
                    tb = Cart.getTable();
                    bool kt = false;
                    foreach (DataRow dr in tb.Rows)
                    {
                        if (Int64.Parse(dr[1].ToString()) == id)
                            kt = true;
                    }
                    if (kt == true)
                        tb = Cart.upDate(tb, id, 1, gia);
                    else
                        tb = Cart.addSP(id, 1, gia);
                    Session["GioHang"] = tb;
                }
                else
                {
                    tb = Cart.init(id, 1, gia);
                    Session["GioHang"] = tb;
                }
                Ultility.Alert("Đã thêm vào giỏ hàng của bạn!", url);
            }
            catch (Exception ee)
            {
                string s = ee.ToString();
                Ultility.Alert("Lỗi!", url);
            }
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public string WordCut(string text)
        {
            return Ultility.WordCut(text, 100, new char[] { ' ', '.', ',', ';' }) + "...";

        }
        public string WordCutTitle(string text)
        {
            return Ultility.WordCut(text, 45, new char[] { ' ', '.', ',', ';' }) + "";

        }
        public string WordCutMoTa(string text)
        {
            return Ultility.WordCut(text, 420, new char[] { ' ', '.', ',', ';' }) + "...";

        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rptXeBanChay.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 6;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rptXeBanChay.Visible = true;
                rptXeBanChay.DataSource = objPds;
                rptXeBanChay.DataBind();
            }
            else
            {
                //lbThongbao.Visible = true;
                //lbThongbao.Text = "Không có sản phẩm!";
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            load();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            load();
        }
    }
}